<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('User');
Globals::requireTable('Product');
Globals::requireTable('Order');
Globals::requireTable('Days');
Globals::requireTable('Account');

class AccountController extends Controller
{
	protected $UserTable;
	protected $ProductTable;
	protected $OrderTable;
	protected $DaysTable;
	protected $AccountTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> false,
		'layoutEnabled'	=> false,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
	    $config['viewEnabled'] = false;
	    $config['layoutEnabled'] = false;
		parent::__construct($config);
		$this->UserTable 	= new UserTable($config);
		$this->ProductTable = new ProductTable($config);
		$this->OrderTable   = new OrderTable($config);
		$this->DaysTable    = new DaysTable($config);
		$this->AccountTable = new AccountTable($config);
	}
	
	public function indexAction()
	{
		
	}
	
	public function listAction()
	{
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    
	    if($uid < 1){
	        $usermodel = new UserModel();
	        $uid = $usermodel->getUserIdByToken($token,$machine_type);
	    }
	    
	    if(empty($uid)){
	        echo json_encode(array('returncode'=>-999,'list'=>array()));
	        exit();
	    }
	    
	    $result = array();
	    $time = time();
	    $products = $this->ProductTable->listAll(array('status'=>1),'rank ASC');
	    foreach ($products as $v){
	        //$ispayed = $this->OrderTable->getRow(array('uid'=>$uid,'status'=>1,'pid'=>$v['id'],"pasttime<{$time}","starttime>{$time}"));
	        
	        $tmp['id'] = $v['id'];
	        $tmp['nameline1'] = $v['name'];
	        $tmp['nameline2'] = $v['name_1'];
	        $tmp['nameline3'] = $v['name_2'];
	        $tmp['imageicon'] = DOMAIN.$v['img'];
	        $tmp['imageright'] = DOMAIN.$v['img_1'];
	        $tmp['imagebg'] = DOMAIN.$v['img_2'];
	        //$tmp['Payed'] = $ispayed['id'] > 0 ? 1 : 0;
	        $result[] = $tmp;
	    }
	    echo json_encode(array('returncode'=>0,'list'=>$result));
	    exit();
	}
	
	public function daylistAction()
	{
	    $uid = $this->getParam('uid');
	    $pid = $this->getIntParam('accountid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getIntParam('machine_type');
	    
	    if($uid < 1){
	        $usermodel = new UserModel();
	        $uid = $usermodel->getUserIdByToken($token,$machine_type);
	    }
	    
	    if(empty($uid) || empty($pid)){
	        echo json_encode(array('returncode'=>-999,'list'=>array()));
	        exit();
	    }
	    
	    $time = time();
	    $ispayed = $this->OrderTable->getRow(array('uid'=>$uid,'status'=>1,'pid'=>$pid,'pasttime>?'=>$time));
	    if($ispayed['id'] > 0){
	        echo json_encode(array('returncode'=>-1,'list'=>array()));
	        exit();
	    }
	    
	    
	    $result = array();
	    $days = $this->DaysTable->listAll(array('pid'=>$pid,'status'=>1),'rank ASC');
	    foreach ($days as $v){
	        $tmp['id'] = $v['id'];
	        $tmp['accountid'] = $pid;
	        $tmp['imageicon'] = DOMAIN.$v['img'];
	        $tmp['imageright'] = DOMAIN.$v['img_1'];
	        $tmp['imagebg'] = DOMAIN.$v['img_2'];
	        $tmp['nameline1'] = $v['name'];
	        $tmp['nameline2'] = $v['name_1'];
	        $tmp['nameline3'] = $v['name_2'];
	        $tmp['money'] = $v['money'];
	        
			$canbuy = $this->AccountTable->getRow(array('pid'=>$pid,'status'=>1,'nums>?'=>0,'endtime'=>0));
			if($canbuy['id'] > 0){
				$tmp['canbuy'] = 1;
			}else{
				$timevalue = (int)$v['days'] * 86400 + strtotime(date('Y-m-d'));
				$canbuy_2 = $this->AccountTable->getRow(array('pid'=>$pid,'status'=>1,'nums>?'=>0,'endtime'=>$timevalue),'id DESC');
				if($canbuy_2['id'] < 1){
					$tmp['canbuy'] = 0;
				}else{
					$accounts = $this->OrderTable->listCount(array('aid'=>$canbuy_2['id'],'status'=>1,'pasttime>?'=>$time));
					$tmp['canbuy'] = $accounts < $canbuy_2['nums'] ? 1 : 0;
				}
			}
			
	        $result[] = $tmp;
	    }
	    echo json_encode(array('returncode'=>0,'list'=>$result));
	    exit();
	}
	
	public function detailAction(){
	    $uid = $this->getParam('uid');
	    $did = $this->getIntParam('dayid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getIntParam('machine_type');
	    
	    if($uid < 1){
	        $usermodel = new UserModel();
	        $uid = $usermodel->getUserIdByToken($token,$machine_type);
	    }
	     
	    if(empty($uid) || empty($did)){
	        echo json_encode(array('returncode'=>-999,'list'=>array()));
	        exit();
	    }
	    
	    $result = array();
	    $days = $this->DaysTable->getRow(array('id'=>$did,'status'=>1));
	    $result['id'] = $days['id'];
	    $result['title'] = $days['title'];
	    $result['nameline1'] = $days['name'];
	    $result['nameline2'] = $days['name_1'];
	    $result['nameline3'] = $days['name_2'];
	    $result['imageicon'] = DOMAIN.$days['img'];
	    $result['imageright'] = DOMAIN.$days['img_1'];
	    $result['imagebg'] = DOMAIN.$days['img_2'];
	    $result['money'] = $days['money'];
	    $result['moneystore'] = $days['money_store'];
	    $result['startdate'] = date('Y-m-d');
	    $result['enddate'] = date('Y-m-d',time() + $days['days'] * 86400);
	    
	    $user = $this->UserTable->getRow($uid);
	    $result['username'] = $user['username'];
	    $result['nickname'] = $user['nickname'];
	    
	    echo json_encode(array('returncode'=>0,'list'=>$result));
	    exit();
	}
	
	protected function out()
	{
		parent::out();
	}
}

Config::extend('AccountController', 'Controller');
